/**
 * vb.wbw
 * 
 * 2012.4.9 实现主界面的页面以及项目中静态元素
 */

package org.dreamfly.dreamalarm;

import java.util.ArrayList;
import java.util.List;

import org.dreamfly.dreamalarm.bean.Alarm;

import com.github.droidfu.activities.BetterDefaultActivity;
import com.umeng.analytics.MobclickAgent;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

abstract class DreamAlarmFrameActivity extends BetterDefaultActivity {

	protected Button newAlarmButton;
	protected ListView alarmListView;
	//微博分享是否开启
	protected boolean isWeiboSharedOn;
	
	int toUpdateId=0;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);

		initFrameModules();
		setClockFrame();
	}

	/**
	 * 初始化界面组件及静态内容
	 */
	private void initFrameModules() {
		newAlarmButton = (Button) findViewById(R.id.main_button_add);
		alarmListView = (ListView) findViewById(R.id.main_alarmlist);
	}

	public final static int NEW_ALARM_REQUEST = 0;

	/**
	 * 初始化闹钟设置界面内容
	 */
	private void setClockFrame() {

		setListViewSource();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	}

	// ------------------------------------------------------------------------------
	// alarm list内容来源，item界面设置
	// ------------------------------------------------------------------------------

	protected List<Alarm> alarmListData; // alarms
	protected ListDataAdapter adapter;

	/**
	 * 设置list数据来源
	 */
	private void setListViewSource() {
		alarmListData = new ArrayList<Alarm>();
		initListData();
		adapter = new ListDataAdapter(this);
		alarmListView.setAdapter(adapter);
	}

	/**
	 * 初始化显示数据
	 */
	abstract void initListData();

	private class ViewHolder {
		CheckBox checkBox;
		TextView textView;
		ImageButton button;
	}

	/**
	 * listview的Adapter，响应CheckBox和DeleteButton的响应事件
	 * 
	 * @author Administrator
	 * 
	 */
	protected class ListDataAdapter extends BaseAdapter {

		private LayoutInflater inflater;
		private ViewHolder holder;

		public ListDataAdapter(Context context) {
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return alarmListData.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return alarmListData.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public void removeItem(int position) {
			// 还没有实现提示用户
			deleteButtonData(alarmListData.get(position).getId());
			alarmListData.remove(position);
			this.notifyDataSetChanged();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if (convertView != null) {
				holder = (ViewHolder) convertView.getTag();
			} else {
				convertView = inflater.inflate(R.layout.alarmlist_items, null);
				holder = new ViewHolder();
				holder.checkBox = (CheckBox) convertView
						.findViewById(R.id.alarmlistitems_checkbox);
				holder.textView = (TextView) convertView
						.findViewById(R.id.alarmlistitems_textview);
				holder.button = (ImageButton) convertView
						.findViewById(R.id.alarmlistitems_deletebutton);
				holder.button.getBackground().setAlpha(00);
				convertView.setTag(holder);
			}

			Alarm alarm = alarmListData.get(position);
			if (alarm != null) {
				if (alarm.getState() == Alarm.TURN_ON) {
					holder.checkBox.setChecked(true);
				} else {
					holder.checkBox.setChecked(false);
				}
				holder.checkBox.setOnClickListener(new CheckBoxListener(
						position));
				holder.textView.setText(alarm.getTimeView());
				holder.button.setOnClickListener(new DeleteButtonListener(
						position));
			}

			return convertView;
		}

		// deleteButton的监听类
		class DeleteButtonListener implements OnClickListener {
			private int position;

			DeleteButtonListener(int pos) {
				position = pos;
			}

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int id = v.getId();
				if (id == holder.button.getId()) {
					removeItem(position);
				}
			}
		}

		class CheckBoxListener implements OnClickListener {
			private int position;

			CheckBoxListener(int pos) {
				position = pos;
			}

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int id = v.getId();
				if (id == holder.checkBox.getId()) {

					toUpdateId=position;
					
					boolean isCheckedBefore = (alarmListData.get(position)
							.getState() == Alarm.TURN_ON);
					Alarm alarm=alarmListData.get(position);
					if(isCheckedBefore){
						alarm.setState(Alarm.TURN_OFF);
					}else{
						alarm.setState(Alarm.TURN_ON);
					}
					alarmListData.set(position, alarm);

					checkBoxDataUpdate(position, !isCheckedBefore);
				}
			}
		}
	}

	/**
	 * 在子类重写，完成deleteButton的delete数据操作
	 * 
	 * @param id
	 */
	abstract void deleteButtonData(int id);

	/**
	 * 子类中重写，完成checkBox变化后的数据操作
	 * 
	 * @param position
	 * @param open
	 */
	abstract void checkBoxDataUpdate(int position, boolean open);

	// ------------------------------------------------------------------------------
	// tools
	// ------------------------------------------------------------------------------

	/**
	 * 利用Toast发布消息
	 */
	abstract void toast(int text, boolean longTime);
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onResume() {
		super.onResume();	
		MobclickAgent.onResume(this);
	}
}